package token;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;

public class test extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        InputStreamReader insr = new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8);
        StringBuilder result = new StringBuilder();
        int respInt = insr.read();
        while (respInt != -1) { // 读取请求数据
            result.append((char) respInt);
            respInt = insr.read();
        }

        System.out.println("前端传递的登录数据为:" + result);
        String token = request.getHeader("token");
        Map<String, String> map = new HashMap<>();
        Enumeration headernames = request.getHeaderNames();
        while (headernames.hasMoreElements()) {
            String key = (String) headernames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        Set<String> ketSet = map.keySet();

        for (Iterator<String> itr = ketSet.iterator(); itr.hasNext(); ) {
            String key = itr.next();
            PrintWriter out = response.getWriter();
            out.println(key + "   " + map.get(key));
            System.out.println(key + "   " + map.get(key));
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
